﻿using System;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using umbraco.interfaces;
namespace Nibble.Umb.Locator.Datatypes.GoogleMapsGeocoder
{
    internal class GoogleMapsGeocoder : Panel, IDataEditor
    {
        private TextBox _address;
        private Button _google;

        private GoogleMapsGeocoderDataType _parent;
        private Artem.Web.UI.Controls.GoogleMap googlemap;

        public GoogleMapsGeocoder(GoogleMapsGeocoderDataType DataType)
        {
            this._parent = DataType;
            this.SetupControls();
                       
        }

        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);
            if (!this.Page.IsPostBack)
            {
                if (this._parent.Data.Value.ToString().Contains(","))
                {
                    string[] coordinates = this._parent.Data.Value.ToString().Split(",".ToCharArray());

                    Artem.Web.UI.Controls.GoogleMarker marker = new Artem.Web.UI.Controls.GoogleMarker(
                        Convert.ToDouble(coordinates[0], Utility.NumberFormatInfo),
                        Convert.ToDouble(coordinates[1], Utility.NumberFormatInfo));
                    marker.Draggable = true;

                    googlemap.Markers.Add(marker);
                    googlemap.Latitude = Convert.ToDouble(coordinates[0], Utility.NumberFormatInfo);
                    googlemap.Longitude = Convert.ToDouble(coordinates[1], Utility.NumberFormatInfo);
                }
                else
                {
                    googlemap.Visible = false;
                }
            }
        }

        public void Save()
        {
            if (googlemap.Markers.Count > 0)
            {
                this._parent.Data.Value =
                    googlemap.Markers[0].Latitude.ToString(Utility.NumberFormatInfo) + "," +
                    googlemap.Markers[0].Longitude.ToString(Utility.NumberFormatInfo);
            }

        }

        private void SetupControls()
        {
            _address = new TextBox();
            _address.ID = _address.ClientID;
            _address.CssClass = "umbEditorTextField";
            _google = new Button();
            _google.ID = _google.ClientID;

            _google.Text = "Google";
            _google.Click += new EventHandler(_google_Click);

            this.Controls.Add(this._address);
            this.Controls.Add(this._google);

            googlemap = new Artem.Web.UI.Controls.GoogleMap();
            googlemap.ID = googlemap.ClientID; 
            googlemap.Key = Utility.GetGoogleMapsKey();
            googlemap.Width = 500;
            googlemap.Height = 300;
            googlemap.Latitude = 42;
            googlemap.Longitude = 24;

            this.Controls.Add(googlemap);


            //AsyncPostBackTrigger apt = new AsyncPostBackTrigger();
            //apt.ControlID = _google.ID;
            //apt.EventName = "Click";

            //this.Triggers.Add(apt);
        }

        void _google_Click(object sender, EventArgs e)
        {
            if (_address.Text.Length > 0)
            {
                googlemap.Address = _address.Text;
                googlemap.Markers.Clear();

                Artem.Web.UI.Controls.GoogleMarker marker = new Artem.Web.UI.Controls.GoogleMarker(_address.Text);
                marker.Text = _address.Text;
                marker.Draggable = true;
                googlemap.Markers.Add(marker);

                googlemap.Visible = true;
            }

           
        }        

        public Control Editor
        {
            get
            {
                return this;
            }
        }

        public virtual bool ShowLabel
        {
            get
            {
                return true;
            }
        }

        public virtual bool TreatAsRichTextEditor
        {
            get
            {
                return false;
            }
        }
    }
}
